import httplib2, amara
from Ft.Lib import Uri

ATOM10_NS = u'http://www.w3.org/2005/Atom'
XHTML_NS = u'http://www.w3.org/1999/xhtml'
ATOMTHR_EXT_NS = u'http://purl.org/syndication/thread/1.0'
COMMON_PREFIXES = { u'atom': ATOM10_NS, u'xh': XHTML_NS, u'thr': ATOMTHR_EXT_NS }

class AppClient(object):
    def __init__(self, service_url=None, username=None, password=None):
        self.h = httplib2.Http('.cache')
        if username and password:
            self.h.add_credentials(username, password)
            self.h.follow_all_redirects = True
        self.service_uri = service_url
        self.xml_base = service_url
#         service_doc = self.load_service_url()
#         if service_doc:
#             self.service = amara.parse(service_doc, prefixes=COMMON_PREFIXES)
#             self.collections = []
#             # print self.service.xml(indent=True)
#             for coll in self.service.service.workspace.collection:
#                 curl = self.get_collection_url(coll)
#                 # print c.xml(indent=True)
#                 c = self.get_collection(curl)
#                 self.collections.append((curl, c))

    def get_collection(self, curl):
        r, c = self.h.request(curl, 'GET')
        print "in get_collection"
        print r
        if r.get('status') == '200':
            if r.get('content-type') == 'application/atom+xml':
                if content:
                    c = amara.parse(c, prefixes=COMMON_PREFIXES)
        return None

    def get_collection_url(self, coll):
        xml_base = ''
        try: xml_base = str(self.service.service.base)
        except: pass
        try: xml_base = str(self.service.service.workspace.base)
        except: pass
        if xml_base:
            curl = Uri.Absolutize(coll, xml_base)
        else:
            curl = coll
        return curl

    def load_service_url(self):
        resp, content = self.h.request(self.service_uri, 'GET')
        print resp
        ok_types = ['application/atomsvc+xml', 'text/xml']
        if resp.get('content-type') in ok_types:
            if resp.get('content-type') != ok_types[0]:
                print "Didn't get an %s content type. Letting it slide for now..." % ok_types[0]
        if resp.get('status') == '200' and content:
            return content
        return None
    
    def put_entry(self, entry_xml, url):
        headers = {'Content-type' : 'application/atom+xml;type=entry'}
        print url
        resp, content = self.h.request(url, "PUT", body=entry_xml, headers=headers)
        return (resp, content)

    def post_entry(self, url, entry_xml, data={}):
        headers = {'Content-type' : 'application/atom+xml;type=entry'}
        if data.get('atom_slug', None):
            headers['slug'] = data['atom_slug']
        print 'Posting to: %s' % url
        resp, content = self.h.request(url, "POST", body=entry_xml, headers=headers)
        return (resp, content)

    def get_entry(self, entry_url):
        headers = {'Accept':  'application/atom+xml'}
        resp, content = self.h.request(entry_url, "GET", headers=headers)
        return (resp, content)

    def delete_entry(self, entry_url):
        pass
